package com.my.demo;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: marshal
 * @date: 2021-08-19 9:35 AM
 */
public class GisUtils {

    /**
     * 获取时间的字符串格式
     *
     * @return
     */
    public static String createFilename(String prefix, String postfix) {
        Date date = new Date();
        SimpleDateFormat sdformat = new SimpleDateFormat("yyyyMMddhhmmssSSS");
        return prefix + sdformat.format(date) + postfix;
    }

    /**
     * 替换参数中的括号
     *
     * @param text
     * @return
     */
    public static String parse(String text) {
        return text.replaceAll("\\[\\[", "").replaceAll("],\\[", ";").replaceAll("]]", "");
    }

    /**
     * 将文本保存到指定文件中
     *
     * @param line
     * @param path
     */
    public static void saveLineToFile(String line, String path) {
        String[] arr = line.split(";");
        List<String> list = Arrays.asList(arr);
        try (FileWriter out = new FileWriter(path)) {
            list.forEach(e -> {
                try {
                    out.write(e + "\n");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String readFileToLine(String path) {
        StringBuilder sb = new StringBuilder("[");
        try (
                FileInputStream fis = new FileInputStream(path);
                InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8.name());
                BufferedReader br = new BufferedReader(isr)
        ) {
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append("[").append(line).append("],");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String str = sb.toString();
        return str.substring(0, str.length() - 1) + "]";
    }
}
